package cn.kgc.controller;

import cn.kgc.bean.pojo.CUser;
import cn.kgc.service.UserManagerService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserManagerController {
    @Autowired
    private UserManagerService ums;

    //查
    @RequestMapping("/selectuser")
    public String selectUser(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CUser> list = ums.selectAll();
        PageInfo<CUser> info = new PageInfo<>(list);
        return JSONObject.toJSONString(list);
    }

    //删
    @RequestMapping(value = "/deleteuser/{id}" ,method = RequestMethod.POST)
    public String deleteUser(@PathVariable long id){
        boolean flag = ums.deleteUserById(id);
        return JSONObject.toJSONString(flag);
    }

    //增
    @RequestMapping(value = "/adduser" ,method = RequestMethod.POST)
    public String adduser(@RequestBody CUser cUser){
        boolean flag = ums.addUser(cUser);
        return JSONObject.toJSONString(flag);
    }

    //改
    @RequestMapping(value = "/updateuser",method = RequestMethod.POST)
    public String updateUser(@RequestBody CUser cUser){
        boolean flag = ums.updateUser(cUser);
        return JSONObject.toJSONString(flag);
    }
    //根据id查询用户详情
    @RequestMapping(value = "/selectuserbyid/{id}", method = RequestMethod.POST)
    public String selectUserById(@PathVariable long id){
        CUser cUser = ums.selectAllById(id);
        return JSONObject.toJSONString(cUser);
    }
}
